package cn.hchaojie.javase.day04;

import java.util.Arrays;

import org.junit.Test;

public class Matrix {
	@Test
	public void test() {
		double[][] matrix = {
			{1.5, 2, 3, 4},
			{2.3, 5, 8, 9},
			{3, 5.4, 7, 10}
		};
		
		System.out.println(sumColumn(matrix, 0));
		System.out.println(sumColumn(matrix, 1));
		System.out.println(sumColumn(matrix, 2));
		
		System.out.println(Arrays.toString(sumColumns(matrix)));
	}
	
	/**
	 * 求矩阵中某一列元素之和
	 * 
	 * @param matrix
	 * @param column
	 * @return
	 */
	public static double sumColumn(double[][] matrix, int column) {
		double sum = 0;
		for (int i = 0; i < matrix.length; i++) {
			sum += matrix[i][column];
		}
		return sum;
	}
	
	public static double[] sumColumns(double[][] matrix) {
		double[] sum = new double[matrix[0].length];
		for (int i = 0; i < matrix[0].length; i++) {
			double temp = 0;
			for (int j = 0; j < matrix.length; j++) {
				temp += matrix[j][i];
			}
			sum[i] = temp;
		}
		
		return sum;
	}
}
